package tq.com.tqstudent.ui.activity.classes;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.lib.common.base.BaseActivity;
import com.lib.common.utils.LogUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;

import butterknife.BindView;
import tq.com.tqstudent.App;
import tq.com.tqstudent.R;
import tq.com.tqstudent.adapter.common.CommonAdapter;
import tq.com.tqstudent.adapter.common.MultiItemTypeAdapter;
import tq.com.tqstudent.adapter.common.ViewHolder;
import tq.com.tqstudent.bean.BaseBean;
import tq.com.tqstudent.bean.IndexBean;
import tq.com.tqstudent.bean.UserInfo;
import tq.com.tqstudent.bean.VisitorBean;
import tq.com.tqstudent.net.ok.DialogCallback;

/**
 * @Author rames
 * @date 2019/3/4 21:44
 */
public class TopicAllActivity extends BaseActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.content_tv)
    TextView contentTV;
    @BindView(R.id.teacher_tv)
    TextView teacherTV;
    private int index; // 学科的序号
    private CommonAdapter adapter;
    private IndexBean.Subject subject;
    private ArrayList<IndexBean.ChapterBean> list = new ArrayList<>();

    public static void show(Activity activity, int index, IndexBean.Subject subject) {
        Intent intent = new Intent(activity, TopicAllActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("index", index);
        bundle.putSerializable("subject", subject);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    @Override
    protected void getExtras(Bundle extras) {
        index = extras.getInt("index");
        subject = (IndexBean.Subject) extras.getSerializable("subject");
    }

    @Override
    public int initLayout() {
        return R.layout.topic_all_activity;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        setTitle("全部内容");

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CommonAdapter<IndexBean.ChapterBean>(this, R.layout.item_vp_class_fragment, list) {
            @Override
            protected void convert(ViewHolder holder, IndexBean.ChapterBean bean, int position) {
                holder.setVisible(R.id.more_layout, false);
                holder.setText(R.id.content_tv, bean.title);
            }
        };
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                TopicListActivity.show(TopicAllActivity.this, list.get(position).id);
            }
        });
        adapter.notifyDataSetChanged();
    }

    private void setViewData(IndexBean.Subject subject){
        contentTV.setText(subject.subject.intro);
        teacherTV.setText(subject.teacher.intro);
        list = subject.chapter;
        adapter.setData(subject.chapter);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void initData() {
        getTKData();
    }

    /** 未登录情况下先调用此接口 */
    private void getTKData(){
        String url = "api/user/chktk";
        OkGo.<BaseBean<VisitorBean>>get(App.getHttpUrl(url))
                .tag(this)
                .execute(new DialogCallback<VisitorBean>(this, false) {
                    @Override
                    public void handleSuccess(VisitorBean data) {
                        if(!TextUtils.isEmpty(data.tk)){
                            UserInfo.setToken(data.tk);
                        }

                        getData();
                    }
                });
    }

    private void getData(){
        String url = "api/clz/index";
        OkGo.<BaseBean<IndexBean>>get(App.getHttpUrl(url))
                .tag(this)
                .execute(new DialogCallback<IndexBean>(this, true) {
                    @Override
                    public void handleSuccess(IndexBean data) {
                        if(null != data && null != data.subject && data.subject.size() >0 ){
                            for (int i = 0; i <data.subject.size() ; i++) {
                                if(data.subject.get(i).subject.id == index){
                                    IndexBean.Subject subject = data.subject.get(i);
                                    setViewData(subject);
                                    return;
                                }
                            }
                        }
                    }

                    @Override
                    public void onError(Response<BaseBean<IndexBean>> response) {
                        setViewData(subject);
                    }
                });
    }
}
